package com.sskj.hangqing.ui.fragment;


import android.support.v4.app.Fragment;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sskj.hangqing.R;
import com.sskj.hangqing.R2;
import com.sskj.hangqing.presenter.HangqingCoinListWrapPresenter;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.TabItem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

@Route(path = RConfig.HANG_FRAGMENT_COIN_LIST_WRAP)
public class HangqingCoinListWrapFragment extends BaseFragment<HangqingCoinListWrapPresenter> {
    @BindView(R2.id.segmentTabLayout)
    CommonTabLayout slidingTabLayout;

    @Autowired
    boolean isUpDown = false;
    @Autowired
    boolean isNew = false;
    @Autowired
    boolean isSelf = false;

    private HashMap<Integer, Boolean> coinListMap;
    private int curCoinPos;

    @Override
    protected int getLayoutId() {
        return R.layout.hang_fragment_coin_list_wrap;
    }

    @Override
    public HangqingCoinListWrapPresenter getPresenter() {
        return new HangqingCoinListWrapPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
    }

    @Override
    protected void initData() {
        mPresenter.getCoinPairs();
    }

    public void onCoinPairs(List<String> data) {
        if (data == null || data.size() == 0) return;

        int size = data.size();
        if (size < 5) {
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) slidingTabLayout.getLayoutParams();
            float onePt = (int) RudenessScreenHelper.pt2px(getContext(), 111.0f);
            lp.width = (int) (onePt * size);
            slidingTabLayout.setLayoutParams(lp);
        }

        List<Fragment> fragments = new ArrayList<>();
        ArrayList<CustomTabEntity> tabItems = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            String tab = data.get(i);
            tabItems.add(new TabItem(tab, 0, 0));
            Fragment fragment = (Fragment) ARouter.getInstance()
                    .build(RConfig.HANG_FRAGMENT_COIN_LIST)
                    .withString(Constans.QU, "/" + tab)
                    .withBoolean("isUpDown", isUpDown)
                    .withBoolean("isNew", isNew)
                    .withBoolean("isSelf", isSelf)
                    .navigation();
            fragments.add(fragment);
        }
        getChildFragmentManager()
                .beginTransaction()
                .add(R.id.fragment_container, fragments.get(0))
                .commitAllowingStateLoss();

        slidingTabLayout.setTabData(tabItems);

        coinListMap = new HashMap<>();
        coinListMap.put(0, true);
        slidingTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (coinListMap.get(position) != null && coinListMap.get(position)) {
                    getChildFragmentManager()
                            .beginTransaction()
                            .hide(fragments.get(curCoinPos))
                            .show(fragments.get(position))
                            .commitAllowingStateLoss();

                } else {
                    getChildFragmentManager()
                            .beginTransaction()
                            .add(R.id.fragment_container, fragments.get(position))
                            .hide(fragments.get(curCoinPos))
                            .show(fragments.get(position))
                            .commitAllowingStateLoss();
                    coinListMap.put(position, true);
                }
                curCoinPos = position;
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
    }
}
